﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ResourceBlender.Core {
    /// <summary>
    /// Allows a resource summary for an application to be bound using XAML.
    /// </summary>
    public class ApplicationSummary {
        /// <summary>
        /// Gets or sets the display name for the application.
        /// </summary>
        /// <value>The display name.</value>
        public string DisplayName { get; set; }
        /// <summary>
        /// Gets or sets the bundles the application contains (formatted).
        /// </summary>
        /// <value>The bundles.</value>
        public List<string> Bundles { get; set; }
        /// <summary>
        /// Gets or sets the total elements.
        /// </summary>
        /// <value>The total elements.</value>
        public int TotalElements { get; set; }
        /// <summary>
        /// Gets or sets the completion of resources in each language.
        /// </summary>
        /// <value>The completion map.</value>
        public List<LanguageCompletion> CompletionMap { get; set; }

        /// <summary>
        /// Initializes a new <see cref="ApplicationSummary"/> with the specified name.
        /// </summary>
        /// <param name="name">The display name.</param>
        public ApplicationSummary(string name) {
            this.DisplayName = name;
            this.Bundles = new List<string>();
            this.TotalElements = 0;
            this.CompletionMap = new List<LanguageCompletion>();
        }

        public override string ToString() {
            return string.Format("{0} ({1} languages)", DisplayName, CompletionMap.Count);
        }



        /// <summary>
        /// A templated column for generating HTML columns.
        /// </summary>
        public class SummaryColumn : INotifyPropertyChanged {
            private string _header,
                           _content;

            /// <summary>
            /// Gets or sets the header template.
            /// </summary>
            /// <value>The header template.</value>
            public string Header {
                get { return _header; }
                set {
                    _header = value;
                    OnPropertyChanged("Header");
                }
            }

            /// <summary>
            /// Gets or sets the content template.
            /// </summary>
            /// <value>The content template.</value>
            public string Content {
                get { return _content; }
                set {
                    _content = value;
                    OnPropertyChanged("Content");
                }
            }

            public SummaryColumn() {
                _header = string.Empty;
                _content = string.Empty;
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string strPropertyName) {
                if(PropertyChanged != null) {
                    PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
                }
            }
        }
    }

    /// <summary>
    /// Shows the resource completion of a language in relation to an application
    /// </summary>
    public class LanguageCompletion : IComparable {
        /// <summary>
        /// Gets or sets the culture this language represents.
        /// </summary>
        /// <value>The culture this language represents.</value>
        public CultureInfo Culture { get; set; }
        /// <summary>
        /// Gets or sets the ISO3166 code used as the language flag image.
        /// </summary>
        /// <value>The ISO3166 code for the language which is used as the flag image).</value>
        public string Image { get; set; }
        /// <summary>
        /// Gets or sets the number translations available in this language.
        /// </summary>
        /// <value>The num translations.</value>
        public int NumTranslations { get; set; }
        /// <summary>
        /// Initializes a new <see cref="LanguageCompletion"/>.
        /// </summary>
        /// <param name="culture">The culture this language represents.</param>
        /// <param name="image">The number translations available in this language.</param>
        /// <param name="numTranslations">The number translations available in this language.</param>
        public LanguageCompletion(CultureInfo culture, string image, int numTranslations) {
            this.Culture = culture;
            this.Image = image;
            this.NumTranslations = numTranslations;
        }

        public override string ToString() {
            return string.Format("{0} ({1} translations)", Culture.EnglishName, NumTranslations);
        }

        /// <summary>
        /// Compares this LanguageCompletion to another object (by English name).
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj) {
            if(obj is LanguageCompletion) {
                return this.Culture.EnglishName.CompareTo((obj as LanguageCompletion).Culture.EnglishName);
            } else {
                return 1;
            }
        }
    }
}